package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupCheckItemMapper;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetmealCheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private SetmealCheckGroupMapper setmealCheckGroupMapper;

    @Override
    @Transactional
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        checkGroupMapper.add(checkGroup);

        Integer checkGroupId = checkGroup.getId();

        checkGroupCheckItemMapper.add(checkitemIds, checkGroupId);
    }

    @Transactional
    @Override
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        checkGroupMapper.edit(checkGroup);

        Integer  checkGroupId = checkGroup.getId();
        checkGroupCheckItemMapper.deleteById(checkGroupId);
        checkGroupCheckItemMapper.add(checkitemIds, checkGroupId);
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        return checkGroup;
    }

    @Transactional
    @Override
    public void deleteById(Integer id) {
        checkGroupCheckItemMapper.deleteById(id);
        checkGroupMapper.deleteById(id);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.selectByCondition(queryPageBean.getQueryString());

        for (CheckGroup checkGroup : page){
            Integer checkGroupId = checkGroup.getId();
            List<Integer> checkItemIds = checkGroupCheckItemMapper.findCheckItemIdsByCheckGroupId(checkGroupId);
            List<CheckItem> checkItems = new ArrayList<>();
            for (Integer checkItemId : checkItemIds){
                checkItems.add(checkItemMapper.findById(checkItemId));
            }
            checkGroup.setCheckItems(checkItems);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> list = checkGroupMapper.findAll();
        return list;
    }

    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        List<Integer> list = setmealCheckGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
        return list;
    }
}
